package org.dev.vlp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dev.vlp.entity.ComUser;
import org.dev.vlp.entity.Community;
import org.dev.vlp.entity.Tag;
import org.dev.vlp.entity.TagCom;
import org.dev.vlp.exception.ApiException;
import org.dev.vlp.mapper.ComUserMapper;
import org.dev.vlp.mapper.TagComMapper;
import org.dev.vlp.service.CommunityService;
import org.dev.vlp.mapper.CommunityMapper;
import org.dev.vlp.vo.CommunityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86184
* @description 针对表【vlp_community】的数据库操作Service实现
* @createDate 2023-01-12 13:52:41
*/
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community>
    implements CommunityService{

    @Autowired
    private TagComMapper tagComMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private ComUserMapper comUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(Community community,int userId) {
        community.setCreatorId(userId);
        if(communityMapper.insert(community)<=0){
            throw new ApiException("创建失败");
        }
        List<Integer> ids=community.getTagIds();
        List<TagCom> tagComs = new ArrayList<>();
        for(Integer tagId:ids){
            TagCom tagCom = new TagCom(community.getComId(), tagId);
            tagComs.add(tagCom);
        }
        if(tagComMapper.insertBatchSomeColumn(tagComs)<=0){
            throw new ApiException("添加标签失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(Community community, int userId) {
        if(!community.getCreatorId().equals(userId)){
            throw new ApiException("无权限修改");
        }
        communityMapper.updateById(community);
        int comId=community.getComId();
        List<Integer> oldTags=tagComMapper.selectTagByComId(comId);
        List<Integer> newTags=community.getTagIds();
        List<Integer> crossList = oldTags.stream().filter(newTags::contains).collect(Collectors.toList());
        List<Integer> old=oldTags.stream().filter(item -> !crossList.contains(item)).collect(Collectors.toList());
        System.out.println(old);
        if(old.size()!=0){
            UpdateWrapper<TagCom> tagComUpdateWrapper = new UpdateWrapper<>();
            tagComUpdateWrapper.set("is_deleted",1).in("com_id",old);
            if(tagComMapper.update(null,tagComUpdateWrapper)<=0){
                throw new ApiException("删除旧标签失败");
            }
        }
        List<Integer> newChange=newTags.stream().filter(item -> !crossList.contains(item)).collect(Collectors.toList());
        System.out.println(newChange);
        if(newChange.size()!=0){
            List<TagCom> tagComs = new ArrayList<>();
            for(Integer in:newChange){
                TagCom tagCom = new TagCom(comId, in);
                tagComs.add(tagCom);
            }
            if(tagComMapper.insertBatchSomeColumn(tagComs)<=0){
                throw new ApiException("新增标签失败");
            }
        }
    }

    @Override
    public void delete(Integer communityId, int userId) {
        Community community = communityMapper.selectById(communityId);
        if(community==null||!community.getCreatorId().equals(userId)){
            throw new ApiException("无权限修改");
        }
        UpdateWrapper<Community> communityUpdateWrapper = new UpdateWrapper<>();
        communityUpdateWrapper.set("is_deleted",1).eq("com_id",communityId);
        if(communityMapper.update(null,communityUpdateWrapper)<=0){
            throw new ApiException("");
        }
        UpdateWrapper<TagCom> tagComUpdateWrapper = new UpdateWrapper<>();
        tagComUpdateWrapper.set("is_deleted",1).eq("com_id",communityId);
        if(tagComMapper.update(null,tagComUpdateWrapper)<=0){
            throw new ApiException("");
        }
    }

    @Override
    public List<Community> selectByName(String name) {
        List<Community> communities = communityMapper.selectByName(name);
        return communities;
    }

    @Override
    public CommunityVo details(int communityId,int userId) {
        CommunityVo details = communityMapper.details(communityId);
        QueryWrapper<ComUser> comUserQueryWrapper = new QueryWrapper<>();
        comUserQueryWrapper.eq("com_id",communityId).eq("user_id",userId);
        ComUser comUser = comUserMapper.selectOne(comUserQueryWrapper);
        details.setIsJoined(comUser != null);
        return details;
    }

    @Override
    public List<Community> getMyCom(int userId) {
        QueryWrapper<Community> communityQueryWrapper = new QueryWrapper<>();
        communityQueryWrapper.eq("creator_id",userId);
        List<Community> communities = communityMapper.selectList(communityQueryWrapper);
        return communities;
    }
}




